home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / blf082b / misc.c < prev    next >
C/C++ Source or Header  |  1993-12-11  |  5KB  |  311 lines

  1. /* 
  2.  *    BloufGate
  3.  *    Misc functions 
  4.  *
  5.  *    Public Domain: may be copied and sold freely
  6.  */
  7.  
  8. #include    "blouf.h"
  9.  
  10. /* 
  11.  * fgets who don't stop when string is full 
  12.  */
  13.  
  14. char *fblfgets(char *str, size_t len, FILE *fl)
  15. {
  16.     int idx=0;
  17.     unsigned char mychar;
  18.     
  19.     while(!feof(fl))
  20.     {
  21.         if(fread(&mychar,1,1,fl)!=1)
  22.             break;
  23.             
  24.         if((size_t)idx<(len-1))
  25.             str[idx++]=(char) mychar;    
  26.         
  27.         if(mychar=='\n')
  28.             break;
  29.     }
  30.     str[idx]=0;
  31.     
  32.     if(idx)
  33.         return str;
  34.  
  35.     return NULL;
  36. }
  37.  
  38. /*
  39.  *    remove ending slash
  40.  */
  41.  
  42. void killslash(char *str)
  43. {
  44.     if(*str)
  45.     {
  46.         if(str[strlen(str)-1]==BLF_DSEPAR)
  47.             str[strlen(str)-1]=0;
  48.     }
  49. }
  50.  
  51. /* 
  52.  *    create rfc file 
  53.  */
  54.  
  55. FILE *create_rfcfile(char *path, char *ext)
  56. {
  57.     FILE *a;
  58.     unsigned long    i=0,loop=0;
  59.     char out[BLFSTR];
  60.  
  61.     i=(unsigned long)time(NULL);
  62.  
  63.     do {
  64.         i++;
  65.         loop++;
  66.         sprintf (out, "%s%cA%lx.%s", path, BLF_DSEPAR, i, ext);
  67.     } while (!access(out,0) && loop<1000);
  68.         
  69.     if(loop>=1000)
  70.     {
  71.         logline("!Can't find a new rfc file name.");
  72.         return NULL;
  73.     }
  74.         
  75.     a = fopen(out,"wb");
  76.     if(!out)
  77.     {
  78.         logline("!Can't open %s",out);    
  79.         return NULL;
  80.     }
  81.     return a;
  82. }
  83.  
  84. /* 
  85.  *    From a message of Burt Juda in the UFGATE echo 
  86.  *
  87.  *    crlf to cr in a string
  88.  *    convert pc accents
  89.  */
  90.  
  91. void mksoftcr (char *text) {
  92.     int c1 = 0;        /* counter since last CR */
  93.     int c2 = 0;       /* counter since last SPACE */
  94.     char *t, *s;
  95.  
  96. /*
  97.  * ++tb: the pointer `s' is anchored to the last space found. Every
  98.  * 78 chars, this space is replaced by a soft break. This could
  99.  * cause problems if no space has been found, ie if the first line of
  100.  * the message is a word longer than that.
  101.  */
  102.    
  103.     s = NULL;
  104.  
  105.     for (t=text; *t; t++)
  106.     {
  107.         c1++;
  108.         c2++;
  109.         if (*t == '\01')
  110.             *t = '-';
  111.         if ((*t=='‚')
  112.             || (*t=='Š')
  113.             || (*t=='ˆ')
  114.             || (*t=='‰')
  115.             || (*t=='Š')) *t='e';
  116.         if ((*t=='Œ')
  117.             || (*t=='')
  118.             || (*t=='‹')) *t='i';
  119.         if ((*t=='—')
  120.             || (*t=='–')) *t='u';
  121.         if ((*t=='…')
  122.             || (*t=='ƒ')) *t='a';
  123.         if (*t=='‡') *t='c';
  124.         if ((*t=='“')
  125.             || (*t=='”')
  126.             || (*t=='•')) *t='o';
  127.       
  128.         /* remove 8th bit */
  129.         *t = *t & 0x7f;
  130.  
  131.         if ((*t == '\n') || (*t == '\r'))
  132.         {
  133.             c1 = 0;/* reset counters */
  134.             c2 = 0;
  135.             continue;
  136.         }
  137.         else
  138.         {
  139.             if ((*t == ' ') || (*t == '\t'))
  140.             {
  141.                 s = t;
  142.                 c2 = 0;
  143.                 continue;
  144.             }
  145.             else
  146.             {
  147.                 if ((c1 > 78) && (s != NULL))
  148.                 {
  149.                     *s = '\n';
  150.                     c1 = c2;
  151.                     continue;
  152.                 }
  153.             }
  154.         }
  155.         /* todo: convert "ansi" graphics to ascii graphics :) */
  156.     }
  157. }
  158.  
  159. /*
  160.  * Remove control codes 
  161.  */
  162.  
  163. void clean_string(char *s)
  164. {
  165.     int i=0;
  166.     
  167.     while(s[i])
  168.     {
  169.         if(s[i]<32)
  170.         {
  171.             if(i==((int) strlen(s)-1)) /* last char */
  172.             {
  173.                 s[i--]=0;
  174.                 break;
  175.             }
  176.             else
  177.                 s[i]=' ';
  178.         }
  179.         /* if(ispunct(s[i])) s[i]='_'; */
  180.         i++;
  181.     }
  182. }
  183.  
  184. /* used to clean a string, config file? */
  185.  
  186. void strip(char *line)
  187. {
  188.     int    i;
  189.  
  190.     if (line[0] != 0) {
  191.         i=0;
  192.         line[strlen(line)-1]='\0';       /* remove ending \n */
  193.         while ((line[i]!='\0') && (line[i]!=';'))
  194.             i++;
  195.         line[i] = '\0'; i--;
  196.         while ((i>=0) && ((line[i]==' ') || (line[i]=='\t')))
  197.             i--;
  198.         line[i+1] = '\0';
  199.     }
  200. }
  201.  
  202. int token(char *tok, char *line) /* !=0 si la ligne commence par token */
  203. {
  204.     int i=0;
  205.     /* case insignifiant */
  206.     while (tok[i]!='\0') {
  207.         if (tolower((int)tok[i]) != tolower((int)line[i]))
  208.             return (0);
  209.         i++;
  210.     }
  211.     while ((line[i]==' ') || (line[i]=='\t'))
  212.         i++;
  213.     return i;
  214. }
  215.  
  216. /* fputiw: read a word in PKT, machine independant byte order */
  217.  
  218. void fputiw (WORD b, FILE *a)
  219. {
  220.     fputc (b & 0xff, a);
  221.     fputc (b >> 8, a);
  222. }
  223.  
  224. /* fgetiw: write a word in PKT */
  225.  
  226. WORD fgetiw (FILE *fi)
  227. {
  228.     int c;
  229.     c = fgetc (fi);
  230.     return (c + (fgetc(fi)<<8));
  231. }
  232.  
  233. void fgetf (char *str, int max, FILE *fi)
  234. {
  235.     unsigned char c;
  236.     int  i=0;
  237.  
  238.     while ((i < max) && ((c=(unsigned char) fgetc(fi)) != '\0')) {
  239.         if ((c != '\n') && ((c-128) != '\n'))
  240.             if (c == '\r') 
  241.                 *str++ = '\n';
  242.             else   
  243.                 *str++ = c;
  244.         i++;
  245.     }
  246.     *str = '\0';
  247.     if(i>=max)
  248.         logline("!fgetf() overflow!");
  249. }
  250.  
  251. /* strscan: return index of char a in line, -1 if not found */
  252. /* fixme: should use strxchr ? */
  253.  
  254. int strscan (char a, char *line)
  255. {
  256.     int i=0;
  257.     while ((*line != a) && (*line != '\0')) { i++; line++; }
  258.     if (*line == '\0') return (-1); else return (i);
  259. }
  260.  
  261. /* logfile routines */
  262.  
  263. FILE *logfile=NULL;
  264.  
  265. int openlog(char *lfname)
  266. {
  267.     logfile=fopen(lfname,"a");
  268.     if(!logfile)
  269.     {
  270.         printf("can't open logfile.\n");
  271.         return FAIL;
  272.     }
  273.     
  274.     return SUCCESS;
  275. }
  276.  
  277. void closelog(void )
  278. {
  279.     if(logfile)
  280.         fclose(logfile);
  281. }
  282.  
  283. /* logline: Log a line in the logfile and output to screen */
  284.  
  285. void logline(const char line[], ...)
  286. {
  287.     time_t    timer;
  288.     char    temp[500], out[500],tdate[20];
  289.     va_list param;
  290.     struct tm *tim;
  291.     
  292.     /* process */
  293.     va_start (param, line);
  294.     vsprintf (temp, line+1, param);
  295.     va_end (param);
  296.     time (&timer);
  297.     tim = localtime (&timer);
  298.     strftime (tdate, 20, "%d %b %H:%M:%S", tim);
  299.     sprintf (out, "%c %s BLF  %s", line[0], tdate, temp);
  300.     
  301.     /* log */
  302.     if(logfile)
  303.     {
  304.         fputs(out,logfile);
  305.         fputc('\n',logfile);
  306.     }
  307.     
  308.     puts(out);
  309. }
  310.  
  311. /* eof */